<?php

include( "./admin/include/func.php");

if (isset( $_POST['login'])) {
	
    $p_name     = trim( strip_tags( $_POST['name']));
    $p_password = trim( strip_tags( $_POST['password']));
       
    $success = true;
    $errors = array();
    
    if ($p_name == '') {
        $success = false;
        array_push( $errors, "A Név nincs megadva!");
    }

    if ($p_password == '') {
        $success = false;
        array_push( $errors, "A Jelszó nincs megadva!");
    }
	                  
    if ($success) {
		
		$db->sql = "SELECT id,nev,bejnev,email,telefon,varos,regio,altkategoria,weblap
					FROM users
					WHERE (bejnev='$p_name' OR email='$p_name') AND jelszo='$p_password' AND aktiv=1
					LIMIT 1";				
		$db->query();
		//print $db->sql;
		
		if (mysql_num_rows( $db->rs) > 0) {
			
			$usr = mysql_fetch_assoc( $db->rs);
			//debug($usr);
			$_SESSION['userid']   = $usr['id'];
			$_SESSION['name']     = $usr['nev'];
			$_SESSION['username'] = $usr['bejnev'];
			$_SESSION['email']    = $usr['email'];	
			$_SESSION['telefon']  = $usr['telefon'];	
			$_SESSION['varos']    = $usr['varos'];	
			$_SESSION['regio']    = $usr['regio'];	
			$_SESSION['rovat']    = $usr['altkategoria'];	
			$_SESSION['weblap']   = $usr['weblap'];	
			
			unset( $_SESSION['error']);
			
			//debug($_SESSION);

		} else {
			array_push( $errors, 'Hibás név és jelszó páros');
			$success = false;					
		}		
    }
    
    if ( ! $success) $_SESSION['error'] = $errors;
}

if (isset( $_GET['logout'])) session_destroy();

header ('Location: ' . $_SERVER['HTTP_REFERER']);

?>
